package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.HandleOrderDto;
import com.huaxiafinance.management.mapper.PackageOrderMapper;
import com.huaxiafinance.management.mapper.PartnerGradeMapper;
import com.huaxiafinance.management.mapper.PartnerInfoMapper;
import com.huaxiafinance.management.model.PackageOrder;
import com.huaxiafinance.management.model.PartnerInfo;
import com.huaxiafinance.management.service.PartnerPassService;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.PartnerUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service("partnerPassService")
public class PartnerPassServiceImpl implements PartnerPassService {
    @Autowired
    private PartnerInfoMapper partnerInfoMapper;
    @Autowired
    private PackageOrderMapper packageOrderMapper;
    @Autowired
    private PartnerUtil partnerUtil;
    @Autowired
    private PartnerGradeMapper partnerGradeMapper;
    private static final Logger log = Logger.getLogger(PartnerPassServiceImpl.class);

    @Override
    @Transactional
    public synchronized MessageResult updateIsPartner(String userUuid) {
        MessageResult result=new MessageResult();
        PartnerInfo info=partnerInfoMapper.queryInfoByUserUuid(userUuid);
        Integer ifPay=packageOrderMapper.queryIfPay(userUuid);
        Integer isPartner=info.getIsPartner();
        Integer minGrade=partnerGradeMapper.selectMinGrade().getId();
        info.setPartnerGradeId(minGrade);
        info.setGmtJoin(new Date());
        info.setGmtApprove(new Date());
        String inviter_code=partnerUtil.getPartnerInviterCode();
        if(isPartner==1){
            result.setFailureResult("已经是合伙人了");
            return result;
        }
        if(ifPay==0){
            result.setFailureResult("没有已支付订单无法成为合伙人");
            return result;
        }
        if(!partnerUtil.isCanUpgrades(userUuid,minGrade)){
            result.setFailureResult("不能达到最低的合伙人等级无法通过");
            return result;
        }

            info.setIsPartner(1);
            info.setInviterCode(inviter_code);
            partnerInfoMapper.updateByPrimaryKey(info);

        result.setSuccessResult();
        return result;
    }

    @Override
    @Transactional
    public synchronized MessageResult handle(HandleOrderDto dto) {
        MessageResult result=new MessageResult();
        Integer id=dto.getId();
        PackageOrder order=packageOrderMapper.selectByPrimaryKey(id);
        if(order.getOrderStatus()!=0){
            result.setFailureResult("当前状态不能处理");
            return result;
        }
        order.setPayEvidenceName(dto.getPayEvidenceName());
        order.setPayEvidence(dto.getPayEvidence());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date=format.parse(dto.getPayTime());
            order.setGmtPay(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        order.setPayType( dto.getPayType());
        order.setOrderStatus(1);
        order.setGmtModified(new Date());
        packageOrderMapper.updateByPrimaryKey(order);
        String userUuid=order.getUserUuid();
        //升级
        partnerUtil.upgrade(userUuid);
        //加提成
        partnerUtil.addMoney(order.getOrderPrice(),order.getPackageType(),order.getUserUuid(),order.getOrderNo());
        //购买合伙人套餐后  变成合伙人
        PartnerInfo info=partnerInfoMapper.queryInfoByUserUuid(userUuid);
        if("Z".equalsIgnoreCase(info.getPartnerType()) && info.getIsPartner()==1 && "P".equalsIgnoreCase(order.getPackageType())){
            info.setPartnerType("P");
            info.setGmtApprove(new Date());
            partnerInfoMapper.updateByPrimaryKey(info);

        }
        try{
            partnerUtil.addScore(order.getPackageId(),userUuid);
        }catch (Exception e){
            log.info("添加积分失败userUuid="+userUuid);
        }



        result.setSuccessResult();

        return result;
    }

    @Override
    public MessageResult updateHandle(HandleOrderDto dto) {
        MessageResult result=new MessageResult();
        Integer id=dto.getId();
        PackageOrder order=packageOrderMapper.selectByPrimaryKey(id);
        if(order.getOrderStatus()!=1){
            result.setFailureResult("当前状态不能修改");
            return result;
        }
        order.setPayEvidenceName(dto.getPayEvidenceName());
        order.setPayEvidence(dto.getPayEvidence());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date=format.parse(dto.getPayTime());
            order.setGmtPay(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        order.setPayType( dto.getPayType());
        packageOrderMapper.updateByPrimaryKey(order);
        result.setSuccessResult();
        return result;
    }


}
